package user;

import control.GoodsFunction;
import utils.AddressBookService;
import utils.DBUtils;
import utils.IAddressBookService;

import java.sql.*;
import java.util.HashMap;
import java.util.Scanner;

public class Menu {
    static HashMap<String, Save> bankdb;//String 键：唯一,Save 储户信息
    Scanner input;
    IAddressBookService addressBookService;

    public Menu() {//构造函数,作用初始化数据成员
        bankdb = new HashMap<>();
        input = new Scanner(System.in);
        addressBookService = new AddressBookService();
    }

    public void runBankSystem() throws Exception {//ctrl+alt+l格式化代码

        String flags = "N";
        do {
            SystemMenu.showMenu();
            String userInput = input.next();
            switch (userInput) {
                case "1" -> { //不用 Break，jdk14 以上
                    browseGoods();//ctrl+鼠标左键
                }
                case "2" -> {
                    orderGoods();
                }
                case "3" -> {
                    pay();
                }
                case "4" -> {
                    refund();
                }
                case "5" -> {
                    administrator();
                }
                case "6" -> {
                    exit();
                }
                default -> {
                    System.out.println("您输入有误！请重试！");
                }
            }
            System.out.println("是否继续操作本系统?(Y|N)");
            flags = input.next();
            SystemMenu.clearScreen();//清屏
        } while (flags.equalsIgnoreCase("y"));
        System.out.println("欢迎下次再来！！");
    }

    //浏览商品
    public static void browseGoods() {
        String sql = "select * from tb_goods";
        try {
            ResultSet resultSet = DBUtils.executeQuery(sql, null);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String type = resultSet.getString("type");
                int inventory = resultSet.getInt("inventory");
                double price = resultSet.getDouble("price");
                System.out.println("ID: " + id + ", Name: " + name + ", type: " + type + ", inventory: " + inventory + ", price:" + price);
            } }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    //下单
    public void orderGoods() {
        System.out.println("正在执行下单功能...loading...");
        System.out.println("---------------------");
        System.out.print("请输入用户名:");
        String name = input.next();
        System.out.print("请输入商品名称:");
        String good = input.next();
        System.out.print("请输入商品数量:");
        int number = input.nextInt();
        System.out.print("请输入商品单价:");
        double price=input.nextDouble();
        double total;
        total=price*number;
        Save save = new Save();
        String accountid = "1122" + (int) (1000 + (Math.random()
                * 9000));
        System.out.println("已生成订单号：" + accountid);
        save.setAccountid(accountid);
        save.setName(name);
        save.setGood(good);
        save.setNumber(number);
        save.setTotal(total);
        bankdb.put(accountid, save);//银行账号
        System.out.println("您已经下单完成!");
        System.out.println("您的订单信息：" + bankdb);
        insert(save);
    }

    public void insert(Save save) {
        int result = addressBookService.insert(save);
        if (result == 1) {
            System.out.println("下单成功！");
        } else {
            System.out.println("下单失败！");
        }
    }

    //支付
    public void pay() throws Exception {
        boolean iscontinue = false;
        int count = 3;
        System.out.println("正在执行支付功能...loading...");
        System.out.println("-------------------");
        System.out.print("请输入您的订单号:");
        String account = input.next();
        if (bankdb.containsKey(account)) {
            Save save = bankdb.get(account);
            do {
                System.out.print("请输入付款金额：");
                double money = input.nextDouble();
                System.out.print("请输入您的密码：");
                String pwd = input.next();
                if (pwd.equals(save.getPwd())) {
                    save.setTotal(save.getTotal() - money);
                    bankdb.put(account, save);
                    if (save.getTotal() > 0) {
                        System.out.println("付款成功！");
                    } else {
                        System.out.println("余额不足，支付失败，即将退出支付界面");
                        Thread.sleep(1000);
                        return;
                    }
                    iscontinue = false;
                } else {
                    //可以添加还有几次机会
                    count--;
                    if (count == 0) {
                        System.out.println("您的密码次数已经用完，即将退出支付功能");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您的密码输入有误,还剩下" +
                            count + "次机会，请重试!");
                    iscontinue = true;
                }
            } while (iscontinue);
        } else {
            System.out.println("您的订单号输入有误！");
            //可以重复几次
        }    }
    //退款

    //退款
    public void refund() throws Exception {
        boolean iscontinue = false;
        int count = 3;
        double money;//初始化？
        System.out.println("正在执行退款...loading...");
        System.out.println("------------------");
        System.out.print("请输入您的订单号:");
        String account = input.next();
        if (bankdb.containsKey(account)) {
            Save save = bankdb.get(account);
            do {
                System.out.println("正在查询订单退款信息，请稍后...loading...");
                Thread.sleep(1000);
                System.out.print("请输入您的密码：");
                String pwd = input.next();
                if (pwd.equals(save.getPwd())) {
                    save.setTotal(save.getMoney() + save.getTotal());
                    bankdb.put(account, save);
                    bankdb.remove(account);
                    System.out.println("退款完成！");
                    iscontinue = false;
                } else {
                    //可以添加还有几次机会
                    count--;
                    if (count == 0) {
                        System.out.println("您的密码次数已经用完，即将退出退款功能");
                        Thread.sleep(1000);
                        return;
                    }
                    System.out.println("您的密码输入有误,还剩下" + count + "次机会，请重试!");
                    iscontinue = true;
                }
            } while (iscontinue);
        } else {
            System.out.println("您的订单号输入有误！");
            //可以重复几次
        }



}

//管理员
public static void administrator() {
    GoodsFunction goodsfunction = new GoodsFunction();
    goodsfunction.runFunction();
}

//退出系统
public void exit() {
    System.out.print("是否真的要退出系统？（Y|N）:");
    String userInput = input.next();
    if (userInput.equalsIgnoreCase("y")) {
        System.exit(0);
    }
}
}




